﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace NNTH
{
    public partial class fBangDiem : DevExpress.XtraEditors.XtraForm
    {
        public fBangDiem()
        {
            InitializeComponent();
        }
        Data.QLTTNNTHEntities db = new Data.QLTTNNTHEntities();
        private void fBangDiem_Load(object sender, EventArgs e)
        {
            this.spinEdit1.Value = DateTime.Now.Year;
            keHoachHTBindingSource.DataSource = db.KeHoachHT;
        }

        private void btnKhoiTao_Click(object sender, EventArgs e)
        {
            if (this.keHoachHTBindingSource.Current!=null)
            {
                var KH=(Data.KeHoachHT)this.keHoachHTBindingSource.Current;
                var list = db.HocVien.Where(p => p.MaLop == KH.MaLop).ToList();
                foreach (var item in list)
                {
                    if (!db.BangDiem.Where(p=>p.MaHV == item.MaHV && p.KHHTID== KH.ID).Any())
                    {
                        var BD = new Data.BangDiem()
                        {
                            KHHTID= KH.ID,
                            MaHV= item.MaHV
                        };
                        db.BangDiem.AddObject(BD);
                    }
                }
                btnLuu.PerformClick();
                bangDiemBindingSource.DataSource = db.BangDiem.Where(p => p.KHHTID == KH.ID && p.HocVien.MaLop == KH.MaLop);
            }
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void keHoachHTBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (this.keHoachHTBindingSource.Current != null)
            {
                var KH = (Data.KeHoachHT)this.keHoachHTBindingSource.Current;
                bangDiemBindingSource.DataSource = db.BangDiem.Where(p => p.KHHTID == KH.ID && p.HocVien.MaLop == KH.MaLop);
            }
           // bangDiemBindingSource.DataSource = db.BangDiem.ư
        }

        private void spinEdit1_EditValueChanged(object sender, EventArgs e)
        {

            keHoachHTBindingSource.DataSource = db.KeHoachHT.Where(p => p.NamHoc == spinEdit1.Value);

        }
    }
}